cc.Class({
    extends: cc.Component,

    properties: {
    },

    ctor () {
        this.points = [14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16];
        this.cardsType = {SINGLE: 0, PAIR: 1, STRAIGHT: 2, FLUSH: 3, FSTRAIGHT: 4, TRIPLE: 5};
        this.cardsTypeName = ["乌龙", "对子", "顺子", "清子", "金条", "三个头"];

        //cards对象设计
        // cards = {
        //     data: [{type: x, point: x}, {type: x, point: x}, {type: x, point: x}],
        //     type: 1,
        //     point: 10,
        //     ...
        // }
    },

    onLoad () {

    },

    start () {

    },

    //对比牌组
    compareComb(comb1, comb2) {
        var compareColors = function(a,b) {
            if (a.colors && b.colors) {
                for (var i = 0, len = a.colors.length; i < len; i++)
                {
                    if (a.colors[i] > b.colors[i])
                        return true;
                    else if (a.colors[i] < b.colors[i])
                        return false;
                }
            }
            return a.color > b.color;
        }

        var comparePoints = function(a,b) {
            for (var i = 0, len = a.points.length; i < len; i++) {
                if (a.points[i] > b.points[i])
                    return true;
                else if (a.points[i] < b.points[i])
                    return false;
            }
            return compareColors(a,b);
        }

        if (comb1.type == comb2.type) {
            if (comb1.points && comb2.points)
                return comparePoints(comb1, comb2);
            if (comb1.point == comb2.point)
                return compareColors(comb1, comb2);
            return comb1.point > comb2.point;
        }
        return comb1.type > comb2.type;
    },

    //获取牌组牌型
    getCombType(cards, force) {
        if (cards.type && !force) return cards.type;

        while (1) {
            if (this.isTriple(cards)) break;
            if (this.isFlushStraight(cards)) break;
            if (this.isFlush(cards)) break;
            if (this.isStraight(cards)) break;
            if (this.isPair(cards)) break;
            cards.type = this.cardsType.SINGLE;
            cards.name = this.cardsTypeName[cards.type];
            cards.point = this.getCombSumPoint(cards);
            cards.color = 0;
            this.sortByPoint(cards);
            cards.points = [this.points[cards.data[0].point], this.points[cards.data[1].point], this.points[cards.data[2].point]];
            cards.colors = [cards.data[0].color, cards.data[1].color, cards.data[2].color];
            break;
        }

        return cards.type;
    },

    //获取牌组累加分数
    getCombSumPoint(cards) {
        var sum = 0;
        for (var i = 0, len = cards.data.length; i < len; i++)
        {
            sum += this.points[cards.data[i].point];
        }
        return sum;
    },

    isTriple(cards) {
        this.sortByPoint(cards);
        if (cards.data[0].point == cards.data[1].point && cards.data[0].point == cards.data[2].point 
            || cards.data[0].color == 4 && cards.data[1].point == cards.data[2].point
            || cards.data[0].color == 4 && cards.data[1].color == 4) {
            cards.type = this.cardsType.TRIPLE;
            cards.name = this.cardsTypeName[cards.type];
            cards.point = cards.data[2].point;
            cards.color = cards.data[2].color;
            return true;
        }
        return false;
    },

    isFlushStraight(cards) {
        this.sortByPoint(cards);
        if ((cards.data[0].color == cards.data[1].color || cards.data[0].color == 4) && cards.data[1].color == cards.data[2].color)
        {
            var is = false;
            if (this.points[cards.data[0].point] == this.points[cards.data[1].point] + 1
                && this.points[cards.data[1].point] == this.points[cards.data[2].point] + 1)
            {
                is = true;
            }
            else if (cards.data[0].color == 4 && this.points[cards.data[1].point] == this.points[cards.data[2].point] + 1)
            {
                is = true;
            }
            else if (cards.data[0].color == 4 && this.points[cards.data[1].point] == this.points[cards.data[2].point] + 2)
            {
                is = true;
            }
            if (!is)
            {
                this.sortByServerPoint(cards);
                if (cards.data[0].point == cards.data[1].point + 1
                    && cards.data[1].point == cards.data[2].point + 1)
                {
                    is = true;
                }
                else if (cards.data[0].color == 4 && cards.data[1].point == cards.data[2].point + 1)
                {
                    is = true;
                }
                else if (cards.data[0].color == 4 && cards.data[1].point == cards.data[2].point + 2)
                {
                    is = true;
                }
            }else{
                cards.type = this.cardsType.FSTRAIGHT;
                cards.name = this.cardsTypeName[cards.type];
                cards.point = cards.data[2].point;
                cards.color = cards.data[2].color;
                return true;
            }
        }
        return false;
    },

    isFlush(cards) {
        this.sortByColor(cards);
        if (cards.data[0].color == cards.data[1].color && cards.data[1].color == cards.data[2].color
            || cards.data[0].color == 4 && cards.data[1].color == cards.data[2].color)
        {
            cards.type = this.cardsType.FLUSH;
            cards.name = this.cardsTypeName[cards.type];
            cards.point = cards.data[0].point;
            cards.color = cards.data[2].color;
            cards.points = [this.points[cards.data[0].point], this.points[cards.data[1].point], this.points[cards.data[2].point]];
            return true;
        }
        return false;
    },

    isStraight(cards) {
        var is = false;
        this.sortByPoint(cards);
        if (this.points[cards.data[0].point] == this.points[cards.data[1].point] + 1
            && this.points[cards.data[1].point] == this.points[cards.data[2].point] + 1)
        {
            is = true;
        }
        else if (cards.data[0].color == 4 && this.points[cards.data[1].point] == this.points[cards.data[2].point] + 1)
        {
            is = true;
        }
        else if (cards.data[0].color == 4 && this.points[cards.data[1].point] == this.points[cards.data[2].point] + 2)
        {
            is = true;
        }
        if (!is)
        {
            this.sortByServerPoint(cards);
            if (cards.data[0].point == cards.data[1].point + 1
                && cards.data[1].point == cards.data[2].point + 1)
            {
                is = true;
            }
            else if (cards.data[0].color == 4 && cards.data[1].point == cards.data[2].point + 1)
            {
                is = true;
            }
            else if (cards.data[0].color == 4 && cards.data[1].point == cards.data[2].point + 2)
            {
                is = true;
            }
        }else{
            cards.type = this.cardsType.STRAIGHT;
            cards.name = this.cardsTypeName[cards.type];
            cards.point = cards.data[2].point;
            cards.color = cards.data[0].color;
            cards.colors = [cards.data[0].color, cards.data[1].color, cards.data[2].color];
            return true;
        }
        return false;
    },

    isPair(cards) {
        this.sortByPoint(cards);
        if (cards.data[0].point == cards.data[1].point || cards.data[1].point == cards.data[2].point || cards.data[0].color == 4)
        {
            cards.type = this.cardsType.PAIR;
            cards.name = this.cardsTypeName[cards.type];
            if (cards.data[1].point == cards.data[2].point)
            {
                var tmp = cards.data[0];
                cards.data.splice(0, 1);
                cards.data.push(tmp);
            }
            cards.point = cards.data[2].point;
            cards.color = cards.data[2].color;
            cards.points = [this.points[cards.data[1].point], this.points[cards.data[2].point]];
            return true;
        }
        return false;
    },

    //找到最佳组合
    findBestCombos(cards) {
        var allCombs = [];
        if (!cards || !cards.data || cards.data.length < 3) return allCombs;
        var getCombPoint = function(comb) {
            return comb[0].type + comb[1].type + comb[2].type;
        };

        var sortComb1 = function(a,b) {
            return (a[0].type + a[1].type + a[2].type) > (b[0].type + b[1].type + b[2].type);
        };
        
        var sortComb2 = function(a,b) {
            return a.type > b.type;
        };

        var compareComb = this.compareComb;

        var firstCombs = [];
        firstCombs = Global.permutation(3, cards.data);
        for (var i = 0, len = firstCombs.length; i < len; i++)
        {
            this.getCombType(firstCombs[i]);
        }
        if (firstCombs.length >= 2) firstCombs.sort(sortComb2);

        var canReturn = false;
        for (var i = 0, len = firstCombs.length; i < len; i++)
        {
            var tmpSrc = {};
            var comb = firstCombs[i];
            // cc.log(cards.data.length, cards.data)
            tmpSrc.data = cards.data.slice(0);
            this.deleteCards(tmpSrc, comb);
            var tmpCombs1 = Global.permutation(3, tmpSrc.data);
            if (!tmpCombs1) tmpCombs1 = [];
            // this.getCombType(comb);
            // cc.log(tmpCombs1, tmpSrc);
            if (comb.type == 1) canReturn = true;
            for (var j = 0, len2 = tmpCombs1.length; j < len2; j++)
            {
                var tmpSrc2 = {};
                var comb1 = tmpCombs1[j];
                tmpSrc2.data = tmpSrc.data.slice(0);
                this.deleteCards(tmpSrc2, comb1);
                var tmpCombs2 = Global.permutation(3, tmpSrc2.data);
                if (!tmpCombs2) tmpCombs2 = [];
                this.getCombType(comb1);
                for (var k = 0, len3 = tmpCombs2.length; k < len3; k++)
                {
                    this.getCombType(tmpCombs2[k]);
                }
                if (tmpCombs2.length >= 2) tmpCombs2.sort(sortComb2);
                for (var k = 0, len3 = tmpCombs2.length; k < len3; k++)
                {
                    var comb2 = tmpCombs2[k];
                    // this.getCombType(comb2);
                    var tmp = [comb, comb1, comb2];
                    if (allCombs.length > 0)
                    {
                        var insert = true;
                        for (var n = 0, len4 = allCombs.length; n < len4; n++)
                        {
                            var comb3 = allCombs[n];
                            // cc.log(comb3, tmp);
                            if (getCombPoint(comb3) == getCombPoint(tmp)) insert = false;
                        }
                        if (insert)
                        {
                            tmp.sort(function(a, b) {return compareComb(a, b)});
                            allCombs.push(tmp);
                        }
                        // cc.log(allCombs.length);
                        if (allCombs.length >= 6 || canReturn)
                        {
                            allCombs.sort(sortComb1);
                            return allCombs;
                        }
                    }
                    else
                    {
                        tmp.sort(function(a, b) {return compareComb(a, b)});
                        allCombs.push(tmp);
                        if (canReturn)
                        {
                            return allCombs;
                        }
                    }
                }
            }
        }
        if (allCombs.length >= 2) allCombs.sort(sortComb1);
        return allCombs;
    },

    sortByColor(cards, isAscend) {
        if (!cards || !cards.data || cards.data.length < 2) return;
        var points = this.points;
        var order = function(a, b) {
            if (a.color == b.color)
                return (points[a.point] < points[b.point]);
            return (a.color < b.color);
        };
        var order2 = function(a, b) {
            if (a.color == b.color)
                return (points[a.point] > points[b.point]);
            return (a.color > b.color);
        }
        if (isAscend)
            cards.data.sort(order);
        else
            cards.data.sort(order2)
    },

    sortByPoint(cards, isAscend) {
        if (!cards || !cards.data || cards.data.length < 2) return;
        var points = this.points;
        var order = function(a, b) {
            if (points[a.point] == points[b.point])
                return (a.color < b.color);
            return (points[a.point] < points[b.point]);
        }
        var order2 = function(a, b) {
            if (points[a.point] == points[b.point])
                return (a.color > b.color);
            return (points[a.point] > points[b.point]);
        }
        if (isAscend)
            cards.data.sort(order);
        else
            cards.data.sort(order2);
    },

    sortByServerColor(cards, isAscend) {
        if (!cards || !cards.data || cards.data.length < 2) return;
        var order = function(a, b) {
            if (a.color == b.color)
                return (a.point < b.point);
            return (a.color < b.color);
        };
        var order2 = function(a, b) {
            if (a.color == b.color)
                return (a.point > b.point);
            return (a.color > b.color);
        }
        if (isAscend)
            cards.data.sort(order);
        else
            cards.data.sort(order2)
    },

    sortByServerPoint(cards, isAscend) {
        if (!cards || !cards.data || cards.data.length < 2) return;
        var order = function(a, b) {
            if (a.point == b.point)
                return (a.color < b.color);
            return (a.point < b.point);
        }
        var order2 = function(a, b) {
            if (a.point == b.point)
                return (a.color > b.color);
            return (a.point > b.point);
        }
        if (isAscend)
            cards.data.sort(order);
        else
            cards.data.sort(order2);
    },

    //剔除
    deleteCards(objCards, delCards, removeAll) {
        var count = 0;
        for (var i = 0, len = delCards.data.length; i < len; i ++)
        {
            for (var j = 0, lenj = objCards.data.length; j < lenj; j++)
            {
                if (objCards.data[j].color == delCards.data[i].color && objCards.data[j].point == delCards.data[i].point)
                {
                    count = count + 1;
                    objCards.data.splice(j, 1);
                    if (!removeAll) break;
                }
            }
        }
        return count;
    },

});
